package com.lagou.controller;

import com.lagou.pojo.Person;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.DispatcherServlet;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@RestController
public class DemoController {

    private final Logger logger = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    private Person person;

    // 热部署原理
    @RequestMapping("/restart")
    public String restart(){
        // jdk.internal.loader.ClassLoaders$AppClassLoader@1f89ab83
        logger.info(DispatcherServlet.class.getClassLoader().toString());
        // org.springframework.boot.devtools.restart.classloader.RestartClassLoader@4ed5856c
        logger.info(this.getClass().getClassLoader().toString());
        return "devtools restart";
    }

    // 热部署排除资源
    @RequestMapping("/restartExclude")
    public String restartExclude(){
        // spring.devtools.restart.exclude=static/*,public/**
        InputStream stream = this.getClass().getClassLoader().getResourceAsStream("static/demo.properties");
        Properties properties = new Properties();
        try {
            properties.load(stream);
        } catch (IOException e) {
            logger.error("配置加载异常:", e);
        }
        return properties.getProperty("key");
    }

    // 自定义config
    @RequestMapping("/config")
    public String config(){
        return person.toString();
    }
}
